package src.greedy_algorithms;

/**
 * @author starsea
 * @date 2024-08-29 9:40
 */

public class Test06 {
    public static void main(String[] args) {
        int c = monotoneIncreasingDigits(556205);
    }

    public static int monotoneIncreasingDigits(int n) {
        String str = Integer.toString(n, 10);
        char[] arr = str.toCharArray();
        int l = 0;
        int r = 0;
        while (l <= r && r < arr.length) {
            if (arr[l] < arr[r]) {
                l=r;
                r++;
            } else if (arr[l] == arr[r]) {
                r++;
            } else {
                arr[l] -= 1;
                l++;
                while (l < arr.length) {
                    arr[l++] = '9';
                }
            }
        }
        String s = new String();
        for (int i = 0; i < arr.length; i++) {
            s += arr[i];
        }
        return Integer.parseInt(s);
    }
}
